<?php

namespace common\models\shop;

use Yii;
use common\helpers\ArrayHelper;
use common\helpers\StringHelper;
use common\helpers\Url;

/**
 * This is the model class for table "{{%shop_category}}".
 *
 * @property int $id
 * @property string $name 分类名称
 * @property string $parent_id 父类ID
 * @property string $icon 分类图标
 * @property string $image 分类图
 * @property string $color 背景色
 * @property int $level 层级
 * @property int $degree 出度
 * @property int $listorder 排序
 * @property int $status 状态
 * @property string $created_at 创建时间
 * @property string $updated_at 更新时间
 * @property string $deleted_at 删除时间
 */
class ShopCategory extends \common\db\ActiveRecord {

    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%shop_category}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['name'], 'required'],
            [['parent_id', 'level', 'degree', 'channel_id', 'listorder', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['name'], 'string', 'max' => 50],
            [['color'], 'string', 'max' => 7],
            [['icon', 'image', 'link'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'name' => '分类名称',
            'parent_id' => '父类ID',
            'icon' => '分类图标',
            'image' => '分类图',
            'level' => '层级',
            'color' => '背景色',
            'degree' => '出度',
            'listorder' => '排序',
            'channel_id' => '关联频道',
            'link' => '外链',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    /**
     *
     */
    public function getChildren() {
        return $this->hasMany(static::className(), ['parent_id' => 'id'])->active()->orderBy([
            'listorder' => SORT_ASC
        ]);
    }

    /**
     *
     */
    public function getParent() {
        return $this->hasOne(static::className(), ['id' => 'parent_id']);
    }

    /**
     * 
     */
    public function getSpread() {
        return $this->hasOne(ShopCategorySpread::className(), ['category_id' => 'id']);
    }

    /**
     * 关联频道ID
     */
    public function getChannel() {
        return $this->hasOne(ShopChannel::className(), ['id' => 'channel_id']);
    }

    public function getProducts() {
        return $this->hasMany(ShopProduct::className(), ['id' => 'product_id'])
        ->viaTable(ShopProductCategory::tableName(), ['category_id' => 'id']);
    }

    public function getProductCount() {
        return $this->getProducts()->count();
    }

    public function getFormatCateNode() {
        $goods_id = 0;
        $type = 'categorys';
        $level = 3;
        $products = $this->products;
        if (count($products) === 1) {
            $goods_id = $products[0]->id;
            $type = 'goods';
        }
        return compact('goods_id', 'type', 'level');
    }

}
